package com.ruoyi.app.controller;

import com.ruoyi.app.domain.ProductCategory;
import com.ruoyi.app.service.IProductCategoryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 商品分类Controller
 *
 * @author zkq
 * @date 2024-07-28
 */
@ApiOperation(value = "商品分类信息", tags = "商品分类信息")
@RestController
@RequestMapping("/product/category")
public class ProductCategoryController extends BaseController
{
    @Autowired
    private IProductCategoryService productCategoryService;

    /**
     * 查询商品分类列表
     */
//    @PreAuthorize("@ss.hasPermi('product:category:list')")
    @GetMapping("/list")
    public AjaxResult list(ProductCategory productCategory)
    {
//        startPage();
        productCategory.setShopId(getShopId());
        List<ProductCategory> list = productCategoryService.selectProductCategoryList(productCategory);
        return  success(getDataTable(list));
    }

    /**
     * 导出商品分类列表
     */
    @PreAuthorize("@ss.hasPermi('product:category:export')")
    @Log(title = "商品分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProductCategory productCategory)
    {
        List<ProductCategory> list = productCategoryService.selectProductCategoryList(productCategory);
        ExcelUtil<ProductCategory> util = new ExcelUtil<ProductCategory>(ProductCategory.class);
        util.exportExcel(response, list, "商品分类数据");
    }

    /**
     * 获取商品分类详细信息
     */
//    @PreAuthorize("@ss.hasPermi('product:category:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(productCategoryService.selectProductCategoryById(id));
    }

    /**
     * 新增商品分类
     */
    @PreAuthorize("@ss.hasPermi('product:category:add')")
    @Log(title = "商品分类", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody ProductCategory productCategory)
    {
        return toAjax(productCategoryService.save(productCategory));
    }

    /**
     * 修改商品分类
     */
    @PreAuthorize("@ss.hasPermi('product:category:edit')")
    @Log(title = "商品分类", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody ProductCategory productCategory)
    {
        return toAjax(productCategoryService.updateById(productCategory));
    }

    /**
     * 添加或修改商品分类
     */
//    @PreAuthorize("@ss.hasPermi('shop:shop/customer:edit')")
    @Log(title = "商品分类", businessType = BusinessType.UPDATE)
    @PostMapping("/addOrUpdate")
    public AjaxResult addOrUpdate(@RequestBody ProductCategory productCategory)
    {
        productCategory.setShopId(getShopId());
        boolean b = productCategoryService.addOrUpdate(productCategory);
        if(b){
            return success(productCategory);
        }
        return error();
    }


    /**
     * 删除商品分类
     */
//    @PreAuthorize("@ss.hasPermi('product:category:remove')")
    @Log(title = "商品分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        List<String> objects = Arrays.asList(ids);
        return toAjax(productCategoryService.removeByIds(objects));
    }

    /**
     * 验证删除商品分类
     * @param id
     * @return
     */
    @Log(title = "商品分类", businessType = BusinessType.DELETE)
    @DeleteMapping("verify/{id}")
    public AjaxResult verifyRemove(@PathVariable String id)
    {
        ProductCategory productCategory = new ProductCategory();
        productCategory.setShopId(getShopId());
        productCategory.setId(id);
        return toAjax(productCategoryService.verifyRemove(productCategory));
    }
}